package cs341.cs.uwosh.edu.jBeats;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Scanner;
import org.apache.tika.exception.TikaException;
import org.xml.sax.SAXException;
import org.apache.commons.io.FileUtils;


public class ClassA {
   JFrame Frame1 = new JFrame("Test Frame");
   JPanel j2 = new JPanel();
   JButton b1 = new JButton ("Click me");
   JTextField t1 = new JTextField(20);
   JPanel j1 = new JPanel (new FlowLayout());
   JButton button = new JButton("Import Song");
   
   
    public ClassA(){
        button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
                    jButton1ActionPerformed(evt);
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(ClassA.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(ClassA.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SAXException ex) {
                    Logger.getLogger(ClassA.class.getName()).log(Level.SEVERE, null, ex);
                } catch (TikaException ex) {
                    Logger.getLogger(ClassA.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        j1.add(button);
        j1.add(t1);
        j2.add(b1);
        Frame1.add(j1);
        Frame1.add(j2);
        Frame1.setLayout(new FlowLayout());
        Frame1.setSize(400,150);
        Frame1.setVisible(true);
        Frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) throws FileNotFoundException, IOException, SAXException, TikaException {                                         
        JFileChooser fc = new JFileChooser();
        fc.showOpenDialog(Frame1);
        File source = fc.getSelectedFile();
        if (source.getName().contains(".mp3")){
            File target = new File("src/cs341/csuwosh/edu/jBeats/Songs"+source.getName());
            System.out.println(target.getAbsolutePath());
            FileUtils.copyFile(source, target);
            DatabaseInfo dbi = new DatabaseInfo();
            
            String path = target.getAbsolutePath().replaceAll("\\" , "/");
            dbi.addSong(path);
        }
        
    } 
    
    public static void main (String [] args){
        new ClassA();
    }
}

